﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lari.Configuration;
using Lari.Framework.Utility;
using Lari.Services.Implementation;

namespace Lari.Web.Utility
{

    public static class HtmlExtension
    {
        #region DropDown items
       
        public static IEnumerable<SelectListItem> ToListItems(this WebViewPage page,LookupEnum lookupType, bool addBlank = true, bool defaultEnable = true,int dependentID=0)
        {
            var mgr = new MasterManager();
            var userMgr = new UserManagementManager();
            var blank = new List<SelectListItem> { new SelectListItem { Text = string.Format("{0} ", string.Empty), Value = "" } };
            try
            {
                switch (lookupType)
                {

                    case LookupEnum.Blank:

                        return blank;
                    
                    case LookupEnum.Title: 

                        if (page.ViewData[DropDownConst.Title] == null)
                        {

                            var items = mgr.Titles().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.ID.ToString(CultureInfo.InvariantCulture) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.Title] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.Title] as IEnumerable<SelectListItem>;

                    case LookupEnum.Designation:

                        if (dependentID!=0)
                        {
                            var dep = mgr.Department(dependentID);
                            if (dep != null && dep.DepDesignationMappings != null && dep.DepDesignationMappings.Count > 0)
                            {
                                blank.AddRange(dep.DepDesignationMappings.Select(desi => new SelectListItem {Text = desi.Designation.Name, Value = desi.Designation.Id.ToString(CultureInfo.InvariantCulture)}));
                                return blank;
                            }
                        }
                        return blank;

                    case LookupEnum.Gender:
                        if (page.ViewData[DropDownConst.Gender] == null)
                        {

                            var items = mgr.Genders().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Code, Selected = (defaultEnable && x.IsDefault) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.Gender] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.Gender] as IEnumerable<SelectListItem>;

                    case LookupEnum.MaritalStatus:
                        if (page.ViewData[DropDownConst.MaritalStatus] == null)
                        {

                            var items = mgr.MaritalStatuses().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Code, Selected = (defaultEnable && x.IsDefault) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.MaritalStatus] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.MaritalStatus] as IEnumerable<SelectListItem>;
                    case LookupEnum.Religion:
                        if (page.ViewData[DropDownConst.Religion] == null)
                        {

                            var items = mgr.Religions().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Code, Selected = (defaultEnable && x.IsDefault) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.Religion] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.Religion] as IEnumerable<SelectListItem>;
                    case LookupEnum.Country:
                        if (page.ViewData[DropDownConst.Country] == null)
                        {

                            var items = mgr.Countries().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Code, Selected = (defaultEnable && x.IsDefault) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.Country] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.Country] as IEnumerable<SelectListItem>;

                    case LookupEnum.Bloodgroup:
                        if (page.ViewData[DropDownConst.Bloodgroup] == null)
                        {

                            var items = mgr.Bloodgroups().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString(CultureInfo.InvariantCulture) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.Bloodgroup] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.Bloodgroup] as IEnumerable<SelectListItem>;

                    case LookupEnum.Department:
                        if (page.ViewData[DropDownConst.Department] == null)
                        {

                            var items = mgr.Departments().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString(CultureInfo.InvariantCulture) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.Department] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.Department] as IEnumerable<SelectListItem>;
                    
                    case LookupEnum.EmploypeeType:
                        if (page.ViewData[DropDownConst.EmployeeType] == null)
                        {

                            var items = mgr.EmployeeTypes().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.ID.ToString(CultureInfo.InvariantCulture)  });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.EmployeeType] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.EmployeeType] as IEnumerable<SelectListItem>;

                    case LookupEnum.State:
                        if (page.ViewData[DropDownConst.State] == null)
                        {

                            var items = mgr.States().OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Code, Selected =(defaultEnable && x.IsDefault) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.State] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.State] as IEnumerable<SelectListItem>;

                    case LookupEnum.Role:
                        if (page.ViewData[DropDownConst.Role] == null)
                        {

                            var items = userMgr.GetAllRoles("*").OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.ID.ToString(CultureInfo.InvariantCulture)});
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.Role] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.Role] as IEnumerable<SelectListItem>;

                    case LookupEnum.QualificationDegree:
                        if (page.ViewData[DropDownConst.QualificationDegree] == null)
                        {

                            var items = mgr.QualificationsAll().Where(q=> q.Parent==null|| q.Parent==0).OrderBy(t => t.Name).ToList().Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString(CultureInfo.InvariantCulture) });
                            if (addBlank)
                            {
                                items = blank.Concat(items);
                                page.ViewData[DropDownConst.QualificationDegree] = items;
                                return items;
                            }
                        }
                        return page.ViewData[DropDownConst.QualificationDegree] as IEnumerable<SelectListItem>;
                        // add new here
                }

            }
            catch
            {
                //todo:
                //ProcessException(MessageCode.User.AddUserFailed, ex, "LariController ToListItems for" + lookupType.ToString());
            }
            return blank;




        }

        #endregion

        #region NVL Function
        public static string NvlFullName(this WebViewPage page, string title, string first, string middle, string last)
        {
            return (string.Format("{0} {1} {2} {3}", title, first, middle, last).Replace("  ", " "));
        }
        public static string Nvl(this WebViewPage page, string data)
        {
            if (string.IsNullOrWhiteSpace(data))
            {
                return "N/A";
            }
            return data;
        }
        public static string Nvl(this WebViewPage page, int data)
        {
            if (data == default(int))
                return "N/A";
            return data.ToString(CultureInfo.InvariantCulture);
        }
        public static string Nvl(this WebViewPage page, int? data)
        {
            if (data == null)
                return "N/A";
            return data.Value.ToString(CultureInfo.InvariantCulture);
        }
        public static string Nvl(this WebViewPage page, DateTime? data)
        {
            if (data == null)
                return "N/A";
            return data.Value.ToShortDateString();
        }
        public static string Nvl(this WebViewPage page, DateTime data)
        {
            return data.ToShortDateString();
        }
        public static string Nvl(this WebViewPage page, bool data)
        {
            return data ? "Y" : "N";
        } 
        #endregion

        #region Lookup info
        public static string LookupInfo(this WebViewPage page, LookupEnum lookupType, string lookupCode)
        {
            const string blank = "N/A";
            var mgr = new MasterManager();
            switch (lookupType)
            {
                case LookupEnum.Country:
                    {
                        var data = mgr.Country(lookupCode);
                        return data != null ? data.Name : blank;
                    }


                case LookupEnum.Gender:
                    {
                        var data = mgr.Genders(lookupCode);
                        return data != null ? data.Name : blank;
                    }
                case LookupEnum.MaritalStatus:
                    {
                        var data = mgr.MaritalStatus(lookupCode);
                        return data != null ? data.Name : blank;
                    }
                case LookupEnum.Religion:
                    {
                        var data = mgr.Religion(lookupCode);
                        return data != null ? data.Name : blank;
                    }
                case LookupEnum.State:
                    {
                        var data = mgr.State(lookupCode);
                        return data != null ? data.Name : blank;
                    }


            }

            return blank;

        }
        public static string LookupInfo(this WebViewPage page, LookupEnum lookupType, int? lookupId)
        {

            const string blank = "N/A";
            if (!lookupId.HasValue)
                return blank;
            var mgr = new MasterManager();

            switch (lookupType)
            {
                case LookupEnum.Bloodgroup:
                    {
                        var data = mgr.Bloodgroup(lookupId.Value);
                        return data != null ? data.Name : blank;
                    }

                case LookupEnum.Designation:
                    {
                        var data = mgr.Designation(lookupId.Value);
                        return data != null ? data.Name : blank;
                    }
                case LookupEnum.Qualification:
                    {
                       var data= mgr.Qualification(lookupId.Value);
                       return data != null ? data.Name : blank;
                    }

            }

            return blank;

        }
        public static string LookupInfo(this WebViewPage page, LookupEnum lookupType, int lookupId)
        {

            const string blank = "N/A";
            var mgr = new MasterManager();
            var userMgr = new UserManagementManager();

            switch (lookupType)
            {

                case LookupEnum.Department:
                    {
                        var data = mgr.Department(lookupId);
                        return data != null ? data.Name : blank;
                    }

                case LookupEnum.EmploypeeType:
                    {
                        var data = mgr.EmployeeType(lookupId);
                        return data != null ? data.Name : blank;
                    }
                case LookupEnum.Title:
                    {
                        var data = mgr.Title(lookupId);
                        return data != null ? data.Name : "";
                    }
                case LookupEnum.Role:
                    {
                        var data = userMgr.RoleOnly(lookupId);
                        return data != null ? data.Name : "";
                    }

            }

            return blank;

        } 
        #endregion

        #region Has access check
        public static bool HasAccess(this WebViewPage page, int accessId)
        {
            return AccessCheck.HasAccess(accessId);
        }
        #endregion
    }
}
